package com.zcc.makingfriend.controller;

import com.zcc.makingfriend.dao.Message;
import com.zcc.makingfriend.service.MessageService;
import com.zcc.makingfriend.untils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author zcc
 */
@Api(tags = "系统消息管理")
@RestController
@RequestMapping("/message")
public class MessageController {
    @Autowired
    MessageService messageService;

    @ApiOperation("发送消息")
    @PostMapping("/send")
    public Result sendMessage(@RequestBody Message message) {
        int i = messageService.addMessage(message);
        if (i == 1) {
            return Result.success("发送成功");
        }
        return Result.fail("发送失败");
    }

    @ApiOperation("阅读消息")
    @GetMapping("/read/{id}")
    public Result readMessage(@PathVariable Long id) {
        int i = messageService.readMessage(id);
        if (i == 1) {
            return Result.success("已阅读");
        }
        return Result.fail("系统错误，阅读失败");
    }

    @ApiOperation("根据用户id获取用户未读取的消息数量")
    @GetMapping("/countUnread/{toId}")
    public Result countUnread(@PathVariable Long toId) {
        return Result.success(messageService.countUnread(toId));
    }

    @ApiOperation("找到消息")
    @GetMapping("/findMessage/{toId}")
    public Result findMessage(@PathVariable Long toId) {
        return Result.success(messageService.findMessage(toId));
    }
}
